draft version.lua (889B)
1 require("io") 2 require("os") 3 4 local draft_version = {} 5 6 draft_version.cache = nil 7 8 local function git_commit() 9 local handle = assert(io.popen("git rev-parse HEAD", "r")) 10 local output = handle:read("*all") 11 local result = {handle:close()} 12 assert(result[3] == 0) 13 return output:sub(1, 8) 14 end 15 16 local function git_uncommited_changes() 17 local handle = assert(io.popen("git status --porcelain", "r")) 18 local output = handle:read("*all") 19 local result = {handle:close()} 20 assert(result[3] == 0) 21 return output ~= "" 22 end 23 24 local function date() 25 return os.date("%Y-%m-%d %H:%M:%S", os.time()) 26 end 27 28 function draft_version.draft_version() 29 if draft_version.cache == nil then 30 local extra = "" 31 if git_uncommited_changes() then 32 extra = "+" 33 end 34 draft_version.cache = "compiled " .. date() .. " commit " .. git_commit() .. extra 35 end 36 tex.print(draft_version.cache) 37 end 38 39 return draft_version